﻿using System.ComponentModel.DataAnnotations;
using memo.entity.PO;
using memo.entity.VO;
using Microsoft.AspNetCore.Mvc;
using memo.Services;
using memo.entity.DTO;

namespace memo.Controllers;


[ApiController]
[Route("[controller]")]
public class UserController : ControllerBase
{

    private readonly UserService _userService;

    public UserController(UserService us) {
        _userService = us;
    }

    

    //增
    [HttpPost("AddUser")]
    public Result<object> AddUser([FromBody] User user) {
        var userId = _userService.AddUser(user);
        return Result<object>.Success(userId);
    }

    //删
    [HttpDelete("DeleteUser")]
    public Result<object> DeleteUser([FromQuery] int id) {
        var result = _userService.DeleteUser(id);
        return Result<object>.Success($"删除成功！删除条数为：{result}");
    }

    //改
    [HttpPut("UpdateUser")]
    public Result<object> UpdateUser([FromBody] User user) {
        if (user.Id == null) {
            return Result<object>.Fail("参数错误");
        }
        var result = _userService.UpdateUser(user);
        return Result<object>.Success($"更新成功！更改条数为：{result}");
    }

    //查
    // 查询所有
    [HttpGet("GetUser")]
    public Result<object> GetUser() {
        var list = _userService.GetAllUsers();
        return Result<object>.Success(list);
    }
    //分页查询所有
    [HttpGet("GetUserByPage")]
    public Result<object> GetMemoByPage([FromQuery, Required] int current, [FromQuery, Required] int size) {
        var vo = _userService.GetUsersByPage(current, size);
        return Result<object>.Success(vo);
    }

    // 条件查询
    [HttpPost("GetUserByCondition")]
    public Result<object> GetUserByCondition([FromBody] UserDto userDto) {
        var vo = _userService.GetUsersByCondition(userDto);
        if(vo == null) {
            return Result<object>.Fail("参数错误");
        }
        return Result<object>.Success(vo);
    }
    
    // 查询指定用户
    [HttpGet("GetUserById")]
    public Result<object> GetUserById([FromQuery, Required] int id) {
        var user = _userService.GetUserById(id);
        return Result<object>.Success(user);
    }
    
    // 用户登录
    [HttpPost("UserLogin")]
    public Result<object> Login([FromQuery, Required] int id, [FromQuery, Required] string password) {
        var userInfo = _userService.Login(id, password);
        if (userInfo == null) {
            return Result<object>.Fail("用户名或密码错误");
        }
        userInfo.Password  = "******";
        return Result<object>.Success(userInfo);
    }


}
